/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tletlis.network;
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *A Threaded class that together with the classes StreamListener and Server will
 * act as a Server on a Client-Server network.
 * The SendMessage class will send a given message to a given number of clients.
 *
 * @author Max
 */
public class SendMessage extends Thread {

    /**
     * The message that will be sent
     */
    private String mess;
    /**
     * A List containing clients that mess will be sent to
     */
    private List<Socket> clientSocketList;
    
    /**
     * Constructor initializes values and start a new thread
     * Creates a new SendMessage object
     * 
     * @param mess
     * @param clients
     */
    public SendMessage(String mess,List<Socket> clients){
            this.mess=mess;
            this.clientSocketList=clients;
            this.start();
    }

    @Override
    /**
     * The overriden run-method. The method will loop through the list of clients
     * and send the message to each one of them.
     */
    public void run(){
        try {
                //Loop that runs through the clientSocketList and send the
                //given message to each client
                for(int i=0; i < clientSocketList.size(); i++){


                    DataOutputStream out = new DataOutputStream(clientSocketList.get(i).getOutputStream());
                    out.writeUTF(mess); out.flush();
               }
        }
                catch (IOException ex) {
                Logger.getLogger(SendMessage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }


